
/*
 * This code was created by Jeff Molofee
 */


#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
//#include "SDL.h"
//#include "SDL_mixer.h"
#include <stdarg.h>
#include <string.h>
#include <time.h>

#include "graphics.h"

 

 
int LoadTGA(TextureImage *inttexture, char *filename)				// Loads A TGA File Into Memory
{
	GLubyte		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};		// Uncompressed TGA Header
	GLubyte		TGAcompare[12];									// Used To Compare TGA Header
	GLubyte		header[6];										// First 6 Useful Bytes From The Header
	GLuint		bytesPerPixel;									// Holds Number Of Bytes Per Pixel Used In The TGA File
	GLuint		imageSize;										// Used To Store The Image Size When Setting Aside Ram
	GLuint		temp;											// Temporary Variable
	GLuint		type=GL_RGBA;									// Set The Default GL Mode To RBGA (32 BPP)

	FILE *file = fopen(filename, "rb");							// Open The TGA File

	if(	file==NULL ||											// Does File Even Exist?
		fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||	// Are There 12 Bytes To Read?
		memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0				||	// Does The Header Match What We Want?
		fread(header,1,sizeof(header),file)!=sizeof(header))				// If So Read Next 6 Header Bytes
	{
		if (file != NULL)											// Did The File Even Exist? *Added Jim Strong*
			fclose(file);											// If Anything Failed, Close The File
		
		return 0;												// Return False
	}

	inttexture->width  = header[1] * 256 + header[0];					// Determine The TGA Width	(highbyte*256+lowbyte)
	inttexture->height = header[3] * 256 + header[2];					// Determine The TGA Height	(highbyte*256+lowbyte)
                                                	
 	if(	inttexture->width	<=0	||										// Is The Width Less Than Or Equal To Zero
		inttexture->height	<=0	||										// Is The Height Less Than Or Equal To Zero
		(header[4]!=24 && header[4]!=32))							// Is The TGA 24 or 32 Bit?
	{
		fclose(file);												// If Anything Failed, Close The File
		return 0;												// Return False
	}

	inttexture->bpp	= header[4];									// Grab The TGA's Bits Per Pixel (24 or 32)
	bytesPerPixel	= inttexture->bpp/8;								// Divide By 8 To Get The Bytes Per Pixel
	imageSize		= inttexture->width*inttexture->height*bytesPerPixel;	// Calculate The Memory Required For The TGA Data

	inttexture->imageData = (GLubyte *)malloc(imageSize);				// Reserve Memory To Hold The TGA Data

	if(	inttexture->imageData==NULL ||									// Does The Storage Memory Exist?
		fread(inttexture->imageData, 1, imageSize, file)!=imageSize)	// Does The Image Size Match The Memory Reserved?
	{
		if(inttexture->imageData!=NULL)								// Was Image Data Loaded
		{
			free(inttexture->imageData);								// If So, Release The Image Data
			inttexture->imageData = NULL;
		}		

		fclose(file);												// Close The File
		return 0;												// Return False
	}

	GLuint i;
	for(i=0; i<(unsigned int)imageSize; i+=bytesPerPixel)	// Loop Through The Image Data
	{																// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
		temp=inttexture->imageData[i];									// Temporarily Store The Value At Image Data 'i'
		inttexture->imageData[i] = inttexture->imageData[i + 2];			// Set The 1st Byte To The Value Of The 3rd Byte
		inttexture->imageData[i + 2] = temp;							// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
	}

	fclose (file);													// Close The File

	// Build A Texture From The Data
	glGenTextures(1, &inttexture[0].texID);							// Generate OpenGL texture IDs

	glBindTexture(GL_TEXTURE_2D, inttexture[0].texID);					// Bind Our Texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered
	
	if (inttexture[0].bpp==24)											// Was The TGA 24 Bits
	{
		type=GL_RGB;												// If So Set The 'type' To GL_RGB
	}

	glTexImage2D(GL_TEXTURE_2D, 0, type, inttexture[0].width, inttexture[0].height,
		0, type, GL_UNSIGNED_BYTE, inttexture[0].imageData);

	return 1;													// Texture Building Went Ok, Return True
}
